<template>
	<view>
		<view class="ipt-wrap">

			<view class="item-wrap">
				<view class="left">
					所属测站
				</view>
				<view class="right">
					<picker @change="stnmPickerChange" :value="nameindex" :range="arraysname">
						<view class="uni-input">{{arraysname[nameindex]}}</view>
					</picker>

				</view>
			</view>
			<view class="item-wrap">
				<view class="left">
					<text style="color:red">*</text>维修时间
				</view>
				<view class="right">
					<picker mode="date" :value="maintenanceTime"   @change="maintenanceTimeChange">
						<view class="uni-input">{{maintenanceTime}}</view>
					</picker>


				</view>
			</view>
			
			<view class="item-wrap">
				<view class="left">
					<text style="color:red">*</text>维修内容
				</view>
				<view class="right" style="width:80%">
					<textarea style="width:100%" v-model="maintenanceContent"  placeholder="请输入维修内容"  auto-height />
					
				</view>
			</view>
			
			
			
			<view class="item-wrap">
				<view class="left">
					备注
				</view>
				<view class="right">
					<input class="uni-input"  v-model="remark"  placeholder="请输入备注" />
				</view>
			</view>
			
			<view class="item-wrap">
				<view class="left">
					附件
				</view>
				<view class="right">
					<button @tap="chooseImg" class="mini-btn" type="primary" size="mini">{{files?'重新上传':'点击上传'}}</button>
			
					
				</view>
			</view>
			<image v-if="files" style="width: 674rpx; height: 358rpx;"  :src="showpic+files" />
		</view>
		<view class="sub-btn">
			
			<image @tap="subdata(true)" style="width: 268rpx; height: 112rpx;"  src="/static/imgnew/tjbg.png" />
		</view>
	</view>
</template>


<script>
	
	export default{
		data(){
			return {
				showpic:this.$showpic,
				files:'',
				remark:'',
				maintenanceContent:'',
				maintenanceTime:'选择时间',
				arraysname:[],
				nameindex:0,
				user:{},
				
				stcdList:[],
				pagedata:{}
				
			}
		},
		onLoad(options){
			
			
			
			
			this.user=JSON.parse(uni.getStorageSync("userinfo"))
			
			
			
			this.stcdList=this.user.stcdList
			
			
			if(options.pagedata){
				this.pagedata=JSON.parse(options.pagedata)
				this.files=this.pagedata.files
				this.pagedata.deviceNo=this.pagedata.deviceNo
				this.files=this.pagedata.files
				this.maintenanceContent=this.pagedata.maintenanceContent
				this.maintenanceTime=this.pagedata.maintenanceTime
				this.remark=this.pagedata.remark
				this.stType=this.pagedata.stType
				for(let i=0;i<this.stcdList.length;i++){
					if(this.stcdList[i].stcd==this.pagedata.stcd){
						this.nameindex=i
					}
				}
				this.stcd=this.pagedata.stcd
				
				
			}
			this.arraysname=this.stcdList.map(item=>{
				return item.stnm
			})
			
		},
		onNavigationBarButtonTap(e) {
			this.subdata()
		},
		methods: {
			chooseImg(){
				
				let that=this
				
				uni.chooseImage({
					count:1,
					sizeType:'compressed',
					success: (res) => {
						const imgsFile=res.tempFilePaths
						uni.showLoading({
						    title: '正在上传'
						});
						const uper=uni.uploadFile({
							url: that.upUrl+'/soilWater/manage/file/create/filesUpload',
							filePath:imgsFile[0],
							name:'files',
							header:{  
								"Authorization": uni.getStorageSync("auth_token")
							},
							success:function(res){
								
								
								let data=JSON.parse(res.data)
								
								console.log(data)
								if(data.code=='1'){
									that.files=data.object
									uni.showToast({
										title: "上传成功",
										icon: "none",
										duration:4000,
									});
									uni.hideLoading();
									
									
									
									
								}
							}
						})
						
					}
				})
			
			},
			maintenanceTimeChange(val){
				this.maintenanceTime=val.detail.value
			},
			stnmPickerChange(val){
				this.nameindex=val.detail.value
			},
			subdata(){
				let that=this
				if(this.maintenanceTime=='选择时间'){
					uni.showToast({
					    title: '请选择时间',
					    duration:4000,
						icon:'none'
					});
					return
				}
				if(this.maintenanceContent==''){
					uni.showToast({
					    title: '请输入维修内容',
					    duration:4000,
						icon:'none'
					});
					return
				}
				
				
				
				that.apipost('/soilWater/manage/maintenance/save/saveMaintenanceRecord',{
					deviceNo:that.deviceNo,
					files:that.files,
					id:that.pagedata.id?that.pagedata.id:'',
					maintenanceContent:that.maintenanceContent,
					maintenanceTime:that.maintenanceTime,
					remark:that.remark,
					stType:that.stcdList[that.nameindex].stType,
					stcd:that.stcdList[that.nameindex].stcd
				},false).then(res => {
					
					
					if(res.code==1){
						uni.showToast({
							icon: 'none',
							duration:4000,
							title: '操作成功'
						});
						setTimeout(function(){
							uni.navigateBack({
								delta:1
							})
						},800)
					}
					
				}).catch(err=>{
					console.log(err)
				});
			}
			
			
		},

	}
</script>
<style>
	.sub-btn{
		display:flex;
		justify-content: center;
		align-items: center;
		padding:40rpx 0 0 0;
	}
	.tip-wrap{
		margin-top:30rpx;
		line-height:54rpx;
		padding-left:40rpx;
	}
	page{
		height:100%;
		background:#F5F9FF;
	}
	.ipt-wrap{
		margin-top:80rpx;
		padding:0 40rpx;
		background:#fff;
	}
	.item-wrap{
		border-bottom:1rpx solid rgba(63,140,255,0.2);;
		color:#00041A;
		height:95rpx;
		display:flex;
		align-items: center;
		justify-content: space-between;
	}
	textarea,input{
		text-align: right;
	}
</style>